//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.chris.serve;

import com.alibaba.fastjson.JSONObject;
import com.chris.impl.registerImpl;
import com.chris.utils.DbConnection;
import com.chris.utils.JsonProcess;
import com.chris.utils.LogUtil;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet({"/registerJournal"})
public class registerJournal extends HttpServlet {
    private final registerImpl dao = new registerImpl();

    public registerJournal() {
    }

    public void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JSONObject read = JsonProcess.read(req.getInputStream());
        PrintWriter writer = resp.getWriter();
        DbConnection.begin();

        try {
            String journal_title = (String)read.get("journal_title");
            int year = (Integer)read.get("year");
            int volume = (Integer)read.get("volume");
            int issue = (Integer)read.get("issue");
            int borrowed = 0;
            this.dao.insert(journal_title, year, volume, issue, borrowed);
            DbConnection.commit();
            writer.print(JsonProcess.respSuccess("添加成功", null));
        } catch (Exception var10) {
            var10.printStackTrace();
            writer.print(JsonProcess.respError("参数错误"));
            LogUtil.log("参数错误",var10);
            DbConnection.rollBack();
        }

    }

    public void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.addHeader("Access-Control-Allow-Headers", "*");
        resp.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT");
    }
}
